package com.example.reggie_take_out.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.reggie_take_out.commen.R;
import com.example.reggie_take_out.entry.Category;
import com.example.reggie_take_out.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 添加新的菜品分类
     * @param category
     * @return
     */
    @PostMapping
    public R<String> add(@RequestBody Category category){
        categoryService.save(category);
        return R.success("菜品添加成功");
    }

    /**
     * 分页查询功能
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize){
        //分页构造器
        Page<Category> pageInfo = new Page<>(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(Category::getSort);

        categoryService.page(pageInfo,wrapper);

        return R.success(pageInfo);
     }

    /**
     * 根据id删除分类
     * @param ids
     * @return
     */

     @DeleteMapping
     public R<String> delete(Long ids){
        categoryService.delete(ids);
        return R.success("删除成功");
     }

    /**
     * 根据id进行数库更新 前端自动完成数据回显功能
     * @param category
     * @return
     */

    @PutMapping
     public R<String> update(@RequestBody Category category){
         categoryService.updateById(category);
         return R.success("修改成功");
     }

    /**
     * 根据传过来的type返回 对应的集合 参数自动封装为Category类
     * @param category
     * @return
     */
     @GetMapping("/list")
     public R<List<Category>> list(Category category){

         LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
         wrapper.eq(category.getType()!=null,Category::getType,category.getType());
         wrapper.orderByDesc(Category::getUpdateTime);

         List<Category> list = categoryService.list(wrapper);
         return R.success(list);

     }



}
